"use strict";

// Contains the nav bar links
var links = [
	"index.html",
	"biography.html",
	"research.html",
	"publications.html",
	"gallery.html"
],

/**
 * Contains functions used in the interactive and styling
 * elements of the web page.
 */
main = {

    /**
     * Formats the navigation panel at the top of all
     * pages.
     */
    createNav: function(navID, menuWidth) {
        return function() {
            var menuItems = $("#" + navID + " span"),
            totalWidths = 0,
            offsets = 0,
            items = 0,
            epsilon = 0;
            
            // Add the total widths of the menu items
            menuItems.each(function(index) {
                totalWidths += $(this).width();
                items++;
            });
            
            // Calculate the fudge amount based on the items
            epsilon = (items * items) / (items - 1);
            
            // Calculate the offsets necessary
            offsets = (menuWidth - totalWidths) / (items - 1) - epsilon;
            
            // Move each item into place
            menuItems.each(function(index) {
                if (index !== 0) {
                    $(this).css("margin-left", offsets);
                }
            });
			
			// Style and add the links
			$(main.bindLinks(navID + " span"));
			
			// Show the invisible navBar
			$("#" + navID).css("visibility", "visible");
        };
    },
    
    /**
     * Adds some bling to the story elements of the page
     */
    setUpStories: function() {
        return function() {
            $(".story").each(function(index) {
                $(this).delay((index + 1) * 200).fadeIn(400);
            });
        };
    },
	
	/**
	 * Helper method to add menu links to a navBar / footer
	 */
	bindLinks: function(navID) {
		return function() {
			$("#" + navID).each(function(index) {
				// Set up event handling and styling
                $(this)
                .hover(function() {
                    // If the user hovers over...
                    $(this).css("border-bottom", "3px solid #000066")
                    .css("color", "#660000");
                }, function() {
                    // When the user moves out...
                    $(this).css("border", "0px")
                    .css("color", "#000000");
                })
                .click(function() {
                    $(this).css("border", "0px")
                    .css("color", "#000000");
                    window.location = links[index];
                });
			});
		};
	},
	
	/**
	 * Adds the "top" buttons to the story heads on longer pages
	 */
    addTop: function(titleID, anchorID) {
		return function() {
			$("." + titleID).each(function(index) {
				$(this).append('<span class="topButton"><a href="#' + anchorID + '">top</a></span>')
			});
		};
	},
	
	/**
	 * Adds the collapsable abstract elements to the publications 
	 */
	addAbstracts: function(entryID) {
		return function() {
			var abstractContent = $("." + entryID),
                currentArticle;
			
			// Find every article with an abstract and add the button
			abstractContent.each(function(index) {
				currentArticle = $(this);
				currentArticle.append('<div class="expandButton">[Abstract]</div>')
				    .children(".expandButton")
					.click((function(currentAbstract) {
						return function() {
							currentAbstract.children(".abstract").slideToggle("slow");
						};
					})(currentArticle));
			});
		}
	}
	
};